<!DOCTYPE html>
<html lang="utf-8">
<head>
    <meta charset="utf-8">
    <#include "/public/page.ftl" />
    <style type="text/css">

    </style>
</head>
<body>
<#include "/public/navbar.ftl" />
<!----/navbar.ftl end---->
<div class="down-main">
    <#assign left_top_key="lhld-manager" />
    <#assign left_menu_key="platform-role" />
    <#include "/public/left.ftl" />
    <!----/left-full end---->
    <div class="right-main right-full">
        <div class="right-container">
            <ul class="breadcrumb">
                <li><a href="${(request.contextPath)!}/main.html">首页</a></li>
                <li><a href="javascript:">系统设置</a></li>
                <li><a href="${(request.contextPath)!}/platform/role/list.html">角色管理</a></li>
                <li class="active">角色授权</li>
            </ul>
            <div class="row-fluid show-grid">
                <div class="span7">
                    <div id="t1">

                    </div>
                </div>
                <div class="span17">
                    <div class="row-fluid row-button-group">
                        <div class="control-group span12">
                            <button type="button" class="button button-small button-primary btn-save">
                                <i class="icon-white icon-ok"></i>保存
                            </button>

                            <button type="button" class="button button-small batch-cancel">
                                <i class="icon-remove"></i>取消
                            </button>
                        </div>
                    </div>
                </div>
            </div>


        </div>
    </div>
    <!--/right-main end -->
</div>
<!----/down-main end---->
<#include "/public/page-script.ftl" />
<script type="text/javascript">
    BUI.use(['bui/tree', 'bui/mask'], function (Tree, Mask) {
        var fullMask = new BUI.Mask.LoadMask({
            el: 'body',
            msg: '正在提交.....'
        });
        var str = '';

        function getNodes(node) {
            if (node.id != null) {
                var id = node.id + ','
                if (str.indexOf(id) >= 0) {
                    str.replace(id, "")
                } else {
                    str += id;
                }
                var p = node.parent
                if (p != null) {
                    getNodes(p);
                }
            }
        }

        //一次性异步加载所有数据
        var storeThree = new BUI.Data.TreeStore({
            root: {
                id: 'lhld',
                text: '系统管理权限'
            },
            pidField: 'pid',
            url: '${(request.contextPath)!}/platform/role/selectThree.html?_roleCode=${map._roleCode!}', //返回的数据如果数据有children字段，且children.length == 0 ，则认为存在未加载的子节点
            //leaf = false，没有children字段也会认为子节点未加载，展开时会自动加载
            autoLoad: true,
            proxy: {
                method: 'post',
            }
        });
        //由于这个树，不显示根节点，所以可以不指定根节点
        var tree = new Tree.TreeList({
            render: '#t1',
            height: gridHeight - 160,
            store: storeThree,
            showRoot: true,
            checkType: 'all', //checkType:勾选模式，提供了4中，all,onlyLeaf,none,custom
            multipleCheck: true, //是否多选，非多选时使用radio
            showLine: true,
        });
        tree.render();
        $(".batch-cancel").on('click', function () {
            window.location.href = '${(request.contextPath)!}/platform/role/list.html';
        });
        $('.btn-save').on('click', function () {
            str = "";
            var checkedNodes = tree.getCheckedNodes();
            BUI.each(checkedNodes, function (node) {
                getNodes(node)
            });
            if (str.length > 0) {
                str = str.slice(0, -1)
            }
            fullMask.show();
            $.ajax({
                type: "post",
                url: '${(request.contextPath)!}/platform/role/permission/save.html',
                dataType: "json",
                data: {
                    _ids: str + "",
                    prCode: "${map._roleCode!}"
                },
                success: function (data) {
                    fullMask.hide();
                    BUI.Message.Show({
                        msg: data.msg,
                        icon: (("1" == data.code) ? 'success' : 'error'),
                        buttons: [{
                            text: '确定',
                            elCls: 'button button-primary',
                            handler: function () {
                                window.location.href = '${(request.contextPath)!}/platform/role/index.html';
                            }
                        }]
                    });
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    fullMask.hide();
                    alert(errorThrown);
                }
            });
        });
    });
</script>
</body>
</html>